package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Collect;
import com.abaaba.project2.bean.CollectExample;
import com.abaaba.project2.bean.Goods;
import com.abaaba.project2.bean.GoodsExample;
import com.abaaba.project2.bean.vo.WxAddDeleteCollectVo;
import com.abaaba.project2.bean.vo.WxCollectSingleVo;
import com.abaaba.project2.bean.vo.WxCollectVo;
import com.abaaba.project2.mapper.CollectMapper;
import com.abaaba.project2.mapper.GoodsMapper;
import com.abaaba.project2.service.WxCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.FastCharsetProvider;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public WxCollectVo getCollectList(Integer userId, Byte type, Integer page, Integer size) {
        PageHelper.startPage(page, size);

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andTypeEqualTo(type).andDeletedEqualTo(false);

        List<Collect> collects = collectMapper.selectByExample(collectExample);

        // TODO type = 1 , 专题 id
        List<Goods> goodsList = new ArrayList<>();
        if (type == 0) {
            for (Collect collect : collects) {
                goodsList.add(goodsMapper.selectByPrimaryKey(collect.getValueId()));
            }
        }

        List<WxCollectSingleVo> wxCollectSingleVos = new ArrayList<>();
        for (int i = 0; i < goodsList.size(); i++) {
            Goods goods = goodsList.get(i);
            Collect collect = collects.get(i);
            wxCollectSingleVos.add(new WxCollectSingleVo(goods.getBrief(), goods.getPicUrl(),
                    collect.getValueId(), goods.getName(), goods.getRetailPrice(),
                    collect.getId(), collect.getType()));
        }

        PageInfo<Collect> wxCollectSingleVoPageInfo = new PageInfo<>(collects);
        int pages = wxCollectSingleVoPageInfo.getPages();

        WxCollectVo wxCollectVo = new WxCollectVo();
        wxCollectVo.setCollectList(wxCollectSingleVos);
        wxCollectVo.setTotalPages(pages);
        return wxCollectVo;
    }

    @Override
    public WxAddDeleteCollectVo addOrDeleteCollect(Integer userId, Byte type, Integer valueId) {
        WxAddDeleteCollectVo wxAddDeleteCollectVo = new WxAddDeleteCollectVo();

        // 根据 userId type valueId 确定唯一的收藏记录
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andTypeEqualTo(type).andValueIdEqualTo(valueId).andUserIdEqualTo(userId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);

        // 若长度为 1 ， 则表明该商品曾经被收藏过
        if (collects.size() == 1) {
            Collect collect = collects.get(0);

            Boolean deleted = collect.getDeleted();
            if (deleted) {
                wxAddDeleteCollectVo.setType("add");
                collect.setDeleted(false);
            } else {
                wxAddDeleteCollectVo.setType("deleted");
                collect.setDeleted(true);
            }
            collectMapper.updateByPrimaryKeySelective(collect);
        } else {
            // 若长度为 0 ， 则表明该商品没有收藏过，需新增该收藏记录
            Collect collect = new Collect(null, userId, valueId, type, new Date(), new Date(), false);
            collectMapper.insert(collect);
            wxAddDeleteCollectVo.setType("add");
        }
        return wxAddDeleteCollectVo;
    }
}
